home *** CD-ROM | disk | FTP | other *** search
/ Languguage OS 2 / Languguage OS II Version 10-94 (Knowledge Media)(1994).ISO / gnu / libg_261.zip / libg_261 / libg++ / old-stream / filebuf.cc < prev    next >
C/C++ Source or Header  |  1992-01-04  |  3KB  |  138 lines

  1. /* 
  2. Copyright (C) 1990 Free Software Foundation
  3.     written by Doug Lea (dl@rocky.oswego.edu)
  4.  
  5. This file is part of GNU CC.
  6.  
  7. GNU CC is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY.  No author or distributor
  9. accepts responsibility to anyone for the consequences of using it
  10. or for whether it serves any particular purpose or works at all,
  11. unless he says so in writing.  Refer to the GNU CC General Public
  12. License for full details.
  13.  
  14. Everyone is granted permission to copy, modify and redistribute
  15. GNU CC, but only under the conditions described in the
  16. GNU CC General Public License.   A copy of this license is
  17. supposed to have been given to you along with GNU CC so you
  18. can know your rights and responsibilities.  It should be in a
  19. file named COPYING.  Among other things, the copyright notice
  20. and this notice must be preserved on all copies.  
  21. */
  22.  
  23. #if 1
  24. #ifdef __GNUG__
  25. #pragma implementation
  26. #endif
  27. #endif
  28.  
  29. #include <std.h>
  30. #include <sys/file.h>           // needed to determine values of O_RDONLY...
  31. #include <filebuf.h>
  32.  
  33. filebuf::filebuf() 
  34.      :streambuf(), fd(-1), opened(0) {}
  35.  
  36. filebuf::filebuf(int newfd) 
  37.      : streambuf(), fd(newfd), opened(1) {}
  38.  
  39. filebuf::filebuf(int newfd, char* buf, int buflen)
  40.      : streambuf(buf, buflen), fd(newfd), opened(1) {}
  41.  
  42. int filebuf::is_open()
  43. {
  44.   return opened;
  45. }
  46.  
  47. int filebuf::close()
  48. {
  49.   int was = opened;
  50.   if (was) ::close(fd);
  51.   opened = 0;
  52.   return was;
  53. }
  54.  
  55. streambuf* filebuf::open(const char* name, open_mode m)
  56. {
  57.   if (opened) return 0;
  58.   int mode = -1; // any illegal value
  59.   switch (m)
  60.   {
  61.   case input: mode = O_RDONLY; 
  62.               break;
  63.   case output: mode = O_WRONLY | O_CREAT | O_TRUNC;
  64.               break;
  65.   case append: mode = O_APPEND | O_CREAT | O_WRONLY;
  66.               break;
  67.   }
  68.   fd = ::open(name, mode, 0666);
  69.   if (opened = (fd >= 0))
  70.   {
  71.     allocate();
  72.     return this;
  73.   }
  74.   else
  75.     return 0;
  76. }
  77.  
  78.  
  79. streambuf*  filebuf::open(const char* filename, io_mode m, access_mode a)
  80. {
  81.   return 0;
  82. }
  83.  
  84. streambuf* filebuf::open(const char* filename, const char* m)
  85. {
  86.   return 0;
  87. }
  88.  
  89. streambuf*  filebuf::open(int  filedesc, io_mode m)
  90. {
  91.   return 0;
  92. }
  93.  
  94. streambuf*  filebuf::open(FILE* fileptr)
  95. {
  96.   return 0;
  97. }
  98.  
  99. int filebuf::underflow()
  100. {
  101.   if (!opened) return EOF;
  102.   if (base == 0) allocate();
  103.   int nwanted = eptr - base + 1;
  104.   int nread = ::read(fd, base, nwanted);
  105.   if (nread >= 0)
  106.   {
  107.     gptr = base;
  108.     pptr = base + nread;
  109.   }
  110.   return (nread <= 0)? EOF : int(*gptr);
  111. }
  112.  
  113. int filebuf::overflow(int ch)
  114. {
  115.   if (!opened) return EOF;
  116.   if (base == 0) allocate();
  117.   if (ch != EOF)             // overflow *must* be called before really full
  118.     *pptr++ = (char)(ch);
  119.  
  120.   // loop, in case write can't handle full request
  121.   // From: Rene' Seindal <seindal@diku.dk>
  122.  
  123.   int w, n, t;
  124.   for (w = t = 0, n = pptr - base; n > 0; n -= w, t += w) 
  125.   {
  126.     if ((w = ::write(fd, base + t, n)) < 0)
  127.       break;
  128.   }
  129.  
  130.   pptr = base;
  131.   return (n == 0 && w >= 0)? 0 : EOF;
  132. }
  133.  
  134. filebuf::~filebuf()
  135. {
  136.   close();
  137. }
  138.